package com.zy.openchat.core.model.voice;

import android.os.Parcel;
import android.os.Parcelable;

import com.zy.openchat.Constants;
import com.zy.openchat.core.model.file.FileInfo;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

public class VoiceInfo implements Parcelable {
    public int duration;
    public FileInfo voice;
    //file_name : "DDD5669709F4450BB8F88A2DF910558D.wav"
    //public String file_name;

    public void updateFile(FileInfo file) {
        if (voice != null&&file!=null&&voice.id == file.id) {
            voice = file;
        }
    }

    public String getRemoteFileId(){
        if(voice!=null&&voice.remote!=null){
            return voice.remote.id;
        }
        return "";
    }

    public long totalSize() {
        if (voice != null)
            return voice.expected_size;
        return 0;
    }

    public long donwloadSize() {
        if (voice != null && voice.local != null)
            return voice.local.downloaded_size;
        return 0;
    }

    public String localAudioPath() {
        if (voice != null && voice.local != null && voice.local.isExist()) {
            return voice.local.path;
        }
        //本地路径，根据file_name获取
//        if (!StringUtils.isEmpty(file_name)) {
//            try {
//                String path = Constants.userAudioPath() + file_name;
//                File f = new File(path);
//                if (f.exists()) {
//                    return path;
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
        return null;
    }

    public boolean isAudioDownloaded() {
        if (voice != null && voice.local != null && voice.local.isExist()) {
            return true;
        }
        //本地路径，根据file_name判断
//        if (!StringUtils.isEmpty(file_name)) {
//            try {
//                String path = Constants.userAudioPath() + file_name;
//                File f = new File(path);
//                if (f.exists()) {
//                    return true;
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
        return false;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.duration);
        dest.writeParcelable(this.voice, flags);
        //dest.writeString(this.file_name);
    }

    public VoiceInfo() {
    }

    protected VoiceInfo(Parcel in) {
        this.duration = in.readInt();
        this.voice = in.readParcelable(FileInfo.class.getClassLoader());
        //this.file_name = in.readString();
    }

    public static final Creator<VoiceInfo> CREATOR = new Creator<VoiceInfo>() {
        @Override
        public VoiceInfo createFromParcel(Parcel source) {
            return new VoiceInfo(source);
        }

        @Override
        public VoiceInfo[] newArray(int size) {
            return new VoiceInfo[size];
        }
    };
}
